package com.itheima.sh.axiosandjson;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

//请求参数和响应都是json格式数据
@WebServlet("/M3Servlet")
public class M3Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            User1 user = JSON.parseObject(request.getInputStream(), User1.class);
            ArrayList<User> list = new ArrayList();
            Collections.addAll(list,new User(4,"s7",18),new User(5,"d3",20),
                    new User(6,"jw",27));
            Result result = new Result(true, "查询列表成功",list);
            response.getWriter().print(JSON.toJSONString(result));
        } catch (IOException e) {
            e.printStackTrace();
            Result result = new Result(false, "查询列表失败");
            response.getWriter().print(JSON.toJSONString(result));
        }


    }
}